﻿Function Start-WindowsService
{ 
    [cmdletbinding(SupportsShouldProcess=$True)]
    Param( 
        [Parameter(Mandatory=$true,ValueFromPipeline=$true)] 
        [String] 
        $Name
         
    )
    Begin
    {
        $watch = [system.diagnostics.stopwatch]::StartNew() 
        write-verbose "Begin Start-WindowsService $Name"
    }
    End
    {
        write-verbose "End Start-WindowsService $Name"
        $watch.Stop();
        Write-Verbose "Start-WindowsService duration : $($watch.Elapsed)"
    }
    Process
    {
        $service = Get-Service -Name $Name -ErrorAction SilentlyContinue
        if ($Service -eq $null)
        {
            Write-Warning "Service $Name doesn't exists"
            return
        }
        
        Start-Service -Name $Name
    }
}

Function Stop-WindowsService
{ 
    [cmdletbinding(SupportsShouldProcess=$True)]
    Param( 
        [Parameter(Mandatory=$true,ValueFromPipeline=$true)] 
        [String] 
        $Name,
        
        [Parameter()]
        [Switch]
        $Force
         
    )
    Begin
    {
        $watch = [system.diagnostics.stopwatch]::StartNew() 
        write-verbose "Begin Stop-WindowsService $Name"
    }
    End
    {
        write-verbose "End Stop-WindowsService $Name"
        $watch.Stop();
        Write-Verbose "Stop-WindowsService duration : $($watch.Elapsed)"
    }
    Process
    {

        $service = Get-Service -Name $Name -ErrorAction SilentlyContinue
        if ($Service -eq $null)
        {
            Write-Warning "Service $Name doesn't exists"
            return
        }
        
        if ($Force)
        {    
            Stop-Service -Name $Name -Force
        }
        else
        {
            Stop-Service -Name $Name            
        }       
    }
}

Function Set-ConfigWindowsService
{ 
    [cmdletbinding(SupportsShouldProcess=$True)]
    Param( 
        [Parameter(Mandatory=$true,ValueFromPipeline=$true)] 
        [String] 
        $Name,

        [Parameter(Mandatory=$true,ValueFromPipeline=$true)] 
        [String] 
        $StartMode
         
    )
    Begin
    {
        $watch = [system.diagnostics.stopwatch]::StartNew() 
        write-verbose "Begin Set-ConfigWindowsService starting mode $StartMode to $Name"
    }
    End
    {
        write-verbose "End Set-ConfigWindowsService starting mode $StartMode to $Name"
        $watch.Stop();
        Write-Verbose "Set-ConfigWindowsService duration : $($watch.Elapsed)"
    }
    Process
    {

    }
}


Function Install-Service
{ 
    [cmdletbinding(SupportsShouldProcess=$True,DefaultParameterSetName="none")]
    Param( 
        [Parameter(Mandatory=$true,ValueFromPipeline=$true,Position=1)] 
        [String] 
        $Name,

        [Parameter(Mandatory=$true,ValueFromPipeline=$true,Position=2)] 
        [String] 
        $Path,

        [Parameter(Mandatory=$true,ParameterSetName='SetCredential')] 
        [String] 
        $UserName,

        [Parameter(Mandatory=$true,ParameterSetName='SetCredential')] 
        [String] 
        $Password
         
    )
    Begin
    {
        $watch = [system.diagnostics.stopwatch]::StartNew() 
        write-verbose "Begin Install-WindowsService $Name from $Path"
        if ($UserName)
        {
            Write-Verbose "Set credential $UserName to service $Name"
        }
    }
    End
    {
        write-verbose "End Install-WindowsService $Name from $Path"
        $watch.Stop();
        Write-Verbose "Install-WindowsService duration : $($watch.Elapsed)"
    }
    Process
    {

    }
}

Function UnInstall-WindowsService
{ 
    [cmdletbinding(SupportsShouldProcess=$True)]
    Param( 
        [Parameter(Mandatory=$true,ValueFromPipeline=$true,Position=1)] 
        [String] 
        $Name,

        [Parameter(Mandatory=$true,ValueFromPipeline=$true,Position=2)] 
        [String] 
        $Path         
    )
    Begin
    {
        $watch = [system.diagnostics.stopwatch]::StartNew() 
        write-verbose "Begin UnInstall-WindowsService $Name from $Path"
    }
    End
    {
        write-verbose "End UnInstall-WindowsService $Name from $Path"
        $watch.Stop();
        Write-Verbose "UnInstall-WindowsService duration : $($watch.Elapsed)"
    }
    Process
    {

    }
}

Function Get-WindowsServiceProperties
{
    Param( 
        [Parameter(Mandatory=$true,ValueFromPipeline=$true)] 
        [String] 
        $Name  
    )
    Begin
    {
        $watch = [system.diagnostics.stopwatch]::StartNew() 
        write-verbose "Begin Get_WindowsServiceProperties $Name"
    }
    End
    {
        write-verbose "End Get-WindowsServiceProperties $Name"
        $watch.Stop();
        Write-Verbose "Get-WindowsServiceProperties duration : $($watch.Elapsed)"
    }
    Process
    {
        
        $service = New-Object System.Object
        $service | Add-Member -type NoteProperty -name Name -value $Name
        $service | Add-Member -type NoteProperty -name DeploymentState -value "Not found"

        #Get the service
        $searchService = get-ciminstance win32_Service -Filter "Name = '$Name'"
        if ($searchService -ne $null)
        {
            $service.DeploymentState ="Found"
            $searchService | Get-Member -MemberType Property | ForEach-Object{
                $service | Add-Member -MemberType NoteProperty -Name $_.Name  -Value $searchService.$($_.Name) -Force -ErrorAction SilentlyContinue
            }


        }

        if($service.PathName)
        {
            $service.PathName = $service.PathName.replace('"','')

            $fileInfo = Get-ItemProperty $service.PathName
            $service | Add-Member -type NoteProperty -name FileVersionInfo -value $fileInfo.VersionInfo.FileVersion
            $service | Add-Member -type NoteProperty -name ProductName -value $fileInfo.VersionInfo.ProductName
            $service | Add-Member -type NoteProperty -name CompanyName -value $fileInfo.VersionInfo.CompanyName
            $service | Add-Member -type NoteProperty -name CreationTime -value $fileInfo.CreationTime
        }
        

        Write-Output $service

    }
}

